//
//  PmtView.m
//  MindBox
//
//  Created by MS on 16-1-3.
//  Copyright (c) 2016年 Lemon. All rights reserved.
//

#import "PmtView.h"
#import "UIView+Frame.h"
#import "ScrollCellView.h"

@interface PmtView ()

@property (nonatomic,strong) NSMutableArray * scrollCellViewArr;

@end

@implementation PmtView
{
    UILabel * _leftTitleView1;
    UILabel * _leftTitleView2;
    UIButton * _rightBtnView;
    UIScrollView * _scrollView;
}

-(NSMutableArray *)scrollCellViewArr
{
    if (_scrollCellViewArr==nil) {
        _scrollCellViewArr = [[NSMutableArray alloc] init];
    }
    return _scrollCellViewArr;
}

-(void)setPmts:(NSArray *)pmts
{
    _pmts = pmts;
    
    int i = 0;
    [_scrollCellViewArr removeAllObjects];
    [self addScrollViewSubViewsWithCount:pmts.count];
    for (Pmt * pmt in pmts) {
        ScrollCellView * cellView = self.scrollCellViewArr[i];
        cellView.pmt = pmt;
        i++;
    }
}

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor =[UIColor whiteColor];
       
        [self addAllChildView];
        
    }
    return self;
}


-(void)addAllChildView
{
    _leftTitleView1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 30, 20)];
    [self addSubview:_leftTitleView1];
    _leftTitleView1.text = @"闪送";
    _leftTitleView1.font = [UIFont systemFontOfSize:12];
    _leftTitleView1.textColor =[UIColor colorWithRed:0.97f green:0.76f blue:0.27f alpha:1.00f];
    
    _leftTitleView2 = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_leftTitleView1.frame), 10, 50, 20)];
    [self addSubview:_leftTitleView2];
    _leftTitleView2.text = @"- 抢购中";
    _leftTitleView2.font = [UIFont systemFontOfSize:12];
    _leftTitleView2.textColor =[UIColor blackColor];
    
    _rightBtnView =[UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:_rightBtnView];
    _rightBtnView.frame = CGRectMake(self.width-50, 10, 50, 20);
    [_rightBtnView setTitle:@"全部" forState:UIControlStateNormal];
    _rightBtnView.titleLabel.font = [UIFont systemFontOfSize:12];
    [_rightBtnView setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [_rightBtnView setImage:[UIImage imageNamed:@"shop_category_cell_arrows"] forState:UIControlStateNormal];
    _rightBtnView.imageEdgeInsets = UIEdgeInsetsMake(0, 30, 0, 0);
    _rightBtnView.titleEdgeInsets = UIEdgeInsetsMake(0, -35, 0, 0);
    [_rightBtnView addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,CGRectGetMaxY(_leftTitleView1.frame)+10 , self.width, 150)];
    [self addSubview:_scrollView];
    
    _scrollView.bounces = NO;
    _scrollView.showsHorizontalScrollIndicator = NO;
   
}

-(void)addScrollViewSubViewsWithCount:(NSInteger)count
{
    CGFloat w = 120;
    CGFloat h = _scrollView.height;
    CGFloat y = 0;
    
    for (int i = 0; i<count; i++) {
        
        ScrollCellView * scrollCellView = [ScrollCellView scrollViewWithXib];
        
        [_scrollView addSubview:scrollCellView];
        scrollCellView.tag =i;
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pmtClick:)];
        [scrollCellView addGestureRecognizer:tap];
        
        [self.scrollCellViewArr addObject:scrollCellView];
        
        CGFloat x = w*i;
        scrollCellView.frame = CGRectMake(x, y, w, h);
    }
    
    _scrollView.contentSize =CGSizeMake(count*w, 0);
 
}

-(void)pmtClick:(UIGestureRecognizer *)tap
{
    NSInteger index = tap.view.tag;
    
    if ([self.delegate respondsToSelector:@selector(pmtViewGoodsDidClick:pmt:index:)]) {
        [self.delegate pmtViewGoodsDidClick:self pmt:self.pmts[index] index:index];
    }
    
}

-(void)btnClick:(UIButton *)btn
{
    if ([self.delegate respondsToSelector:@selector(pmtAllBtnDidClick:)]) {
        
        [self.delegate pmtAllBtnDidClick:self];
    }
    
}

@end
